<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>成绩表</title>
    <style>
        [v-cloak] {
            display: none;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        em {
            font-style: normal;
        }

        a {
            text-decoration: none;
        }

        section.container {
            display: flex;
            justify-content: space-between;
            width: 800px;
            margin: 100px auto;
        }

        table,
        th,
        td {
            border: 1px solid #b6b1b1;
            border-collapse: collapse;
            text-align: center;
        }

        th {
            width: 120px;
            background: rgb(229, 225, 225);
        }


        th,
        td {
            padding: 5px 0;
        }

        button {
            border: none;
            background: skyblue;
            color: #fff;
            padding: 5px 20px;
        }
    </style>
</head>

<body>
    <div id="container" v-cloak>
        <section class="container">
            <table>
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>科目</th>
                        <th>成绩</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody v-if="arr.length > 0">
                    <tr v-for="(v,i) in arr" :key="v.id">
                        <td>{{i+1}}</td>
                        <td>{{v.subject}}</td>
                        <td :style="v.score < 60 ? {color: 'red'} : ''">{{v.score}}</td>
                        <td><a @click="del(v.id)" href="javascript:void(0)">删除</a></td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="4">
                            <div>
                                <span>总分：</span><em>{{total}}</em>
                                <span>平均分：</span><em>{{avg}}</em>
                            </div>
                        </td>
                    </tr>
                </tfoot>
            </table>

            <aside>
                <div>
                    <span>科目：</span><input type="text" name="subject" id="" placeholder="请输入科目" v-model.trim="subject">
                </div>
                <div>
                    <span>成绩：</span><input type="text" name="score" id="" placeholder="请输入成绩" v-model.number="score">
                </div>
                <button @click="add">添加</button>
            </aside>
        </section>
    </div>

    <script src="./js/dev/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#container',
            data: {
                // info: {
                // id: 0,
                subject: '',
                score: '',
                // },

                arr: []
            },
            methods: {
                del(id) {
                    this.arr = this.arr.filter(e => e.id !== id)
                },
                add() {
                    const info = { id: +new Date(), subject: this.subject, score: this.score }
                    this.arr.push(info)
                    this.subject = ''
                    this.score = ''
                }
            },
            computed: {
                total() {
                    return this.arr.reduce((pre, next) => pre + next.score, 0)
                },
                avg() {
                    return this.total && this.total / this.arr.length
                }
            }
        })
    </script>
</body>

</html>